package com.miguo.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class AlarmSign {
    public static final String PUSH_FLAG_HAS = "push_flag_has";
    public static final String RA_ACTION = "com.miguo.action.RA_ACTION_".concat("miguo");
    public static final int REQUEST_BROADCAST_TYPE = 0;
    public static final int REQUEST_ACTIVITY_TYPE = 1;
    public static final int REQUEST_SERVICE_TYPE = 2;
    private AlarmManager mAlarmManager;
    private Context mContext;

    public AlarmSign(Context context) {
        mContext = context;
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    }

    public void cancelAlarmActivityTime(Intent intent) {
        mAlarmManager.cancel(PendingIntent.getActivity(mContext, REQUEST_ACTIVITY_TYPE, intent, 0));
    }

    public void cancelAlarmServiceTime(Intent intent) {
        mAlarmManager.cancel(PendingIntent.getService(mContext, REQUEST_SERVICE_TYPE, intent, 0));
    }

    public void cancelAlarmTime(Intent intent) {
        mAlarmManager.cancel(PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, 0));
    }

    public PendingIntent getSignedPending(Intent intent) {
        return PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, PendingIntent.FLAG_NO_CREATE);
    }

    public void setAlarmTime() {
        Intent intent = new Intent(RA_ACTION);
        if (getSignedPending(intent) == null) {
            PendingIntent operation = PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, 0);
            mAlarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 7200000L, 7200000L, operation);
        }
    }

    public void setAlarmTime(String action, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(action);
        PendingIntent operation = PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, 0);
        mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
    }

    public void setAlarmTimeForAction(String action, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(action);
        if (getSignedPending(intent) == null) {
            PendingIntent operation = PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, 0);
            mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
        }
    }

    public void setAlarmTimeForActivity(Class cls, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(mContext, cls);
        intent.setAction(String.valueOf(triggerAtMillis));
        if (getSignedPending(intent) == null) {
            PendingIntent operation = PendingIntent.getActivity(mContext, REQUEST_ACTIVITY_TYPE, intent, 0);
            mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
        }
    }

    public void setAlarmTimeForService(Class cls, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(mContext, cls);
        intent.setAction(String.valueOf(triggerAtMillis));
        if (getSignedPending(intent) == null) {
            PendingIntent operation = PendingIntent.getService(mContext, REQUEST_SERVICE_TYPE, intent, 0);
            mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
        }
    }

    public void setAlarmTimeForService(Class cls, String action, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(mContext, cls);
        intent.setAction(action);
        if (getSignedPending(intent) == null) {
            PendingIntent operation = PendingIntent.getService(mContext, REQUEST_SERVICE_TYPE, intent, 0);
            mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
        }
    }

    public void setAlarmTimeForServiceOnce(Class cls, long action) {
        Intent intent = new Intent(mContext, cls);
        intent.setAction(String.valueOf(action));
        if (getSignedPending(intent) == null) {
            PendingIntent operation = PendingIntent.getService(mContext, REQUEST_SERVICE_TYPE, intent, 0);
            mAlarmManager.set(AlarmManager.RTC, action, operation);
        }
    }

    public void setAlarmTimeOnce(Intent intent, long triggerAtMillis) {
        PendingIntent operation = PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.set(AlarmManager.RTC, triggerAtMillis, operation);
    }

    public void updateAlarmTimeForAction(String action, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(action);
        PendingIntent operation = PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
        mAlarmManager.set(AlarmManager.RTC, triggerAtMillis, operation);
    }

    public void updateAlarmTimeForActionOnce(String action, long paramLong) {
        Intent intent = new Intent(action);
        PendingIntent operation = PendingIntent.getBroadcast(mContext, REQUEST_BROADCAST_TYPE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.set(AlarmManager.RTC, paramLong, operation);
    }

    public void updateAlarmTimeForService(Class cls, String action, long triggerAtMillis, long intervalMillis) {
        Intent intent = new Intent(mContext, cls);
        intent.setAction(action);
        PendingIntent operation = PendingIntent.getService(mContext, REQUEST_SERVICE_TYPE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, operation);
    }
}

